<?php

declare(strict_types=1);

namespace App\Service;

use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Library\Common;
use App\Model\DadaShop;
use App\Model\data\CurrentShopData;
use App\Model\FlyBusinessDistrictLinkShop;
use App\Model\FlyShop;
use App\Model\FlyShopOpenTime;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Validation\Rule;
use Hyperf\Validation\ValidatorFactory;
use Hyperf\Di\Annotation\Inject;

/**
 * 店铺配置
 * Class ShopService
 * @package App\Service
 */
class ShopService extends BaseService
{

    /**
     * @Inject
     * @var ShopDeliveryConfigService
     */
    private $deliveryConfig;

    /**
     * @Inject
     * @var RequestInterface
     */
    private $request;

    /**
     * @Inject
     * @var ValidatorFactory
     */
    private $validator;

    /**
     * 美食商铺列表
     * @param array $param
     * @return object
     */
    public function food_list($param)
    {
        $where = [];
        $where['fly_shop.food_state'] = 1;
        $where['fly_shop.food_switch'] = 1;
        return  FlyShop::query()->where($where);
        $district_id = $request_p['district_id'];
        if($district_id){
            $key_word = $request_p['key_word'];
            $lat = floatval($request_p['lat']);
            $lng = floatval($request_p['lng']);
            $where['fly_business_district_link_shop.district_id'] = $district_id;
            return FlyBusinessDistrictLinkShop::query()
                ->leftJoin('fly_shop','fly_shop.id','=','fly_business_district_link_shop.shop_id')
                ->where($where)
                ->where('fly_shop.food_state',1)
                ->when($key_word,function ($query) use($key_word){
                    $query->where('fly_business_district_link_shop.name','like','%'.$key_word.'%');
                })
                ->select(['*', Db::raw(" TRUNCATE(st_distance_sphere(point($lng,$lat),point(lng,lat))/1000,2) as distance_meter")])
                ->orderBy('distance_meter');;
        }else{
            return FlyShop::query()
                ->where($where)
                ->when($key_word,function ($query) use($key_word){
                    $query->where('fly_shop.shop_name','like','%'.$key_word.'%');
                })
                ->select(['*', Db::raw(" TRUNCATE(st_distance_sphere(point($lng,$lat),point(lng,lat))/1000,2) as distance_meter")])
                ->orderBy('distance_meter');
        }

    }

    /**
     * 商铺列表
     * @param array $param
     * @return object
     */
    public function shop_list($param)
    {
        $where = [];
        $where['fly_shop.shop_state'] = 1;
        $district_id = $param['district_id'];
        $lat = floatval($param['lat']);
        $lng = floatval($param['lng']);
        $key_word = $param['key_word'];
        if($district_id){
            $where['fly_business_district_link_shop.district_id'] = $district_id;
            return FlyBusinessDistrictLinkShop::query()
                ->leftJoin('fly_shop','fly_shop.id','=','fly_business_district_link_shop.shop_id')
                ->where($where)
                ->where('fly_shop.food_state',1)
                ->when($key_word,function ($query) use($key_word){
                    $query->where('fly_business_district_link_shop.name','like','%'.$key_word.'%');
                })
                ->select(['*', Db::raw(" TRUNCATE(st_distance_sphere(point($lng,$lat),point(lng,lat))/1000,2) as distance_meter")])
                ->orderBy('distance_meter');;
        }else{
            return FlyShop::query()
                ->where($where)
                ->when($key_word,function ($query) use($key_word){
                    $query->where('fly_shop.shop_name','like','%'.$key_word.'%');
                })
                ->select(['*', Db::raw(" TRUNCATE(st_distance_sphere(point($lng,$lat),point(lng,lat))/1000,2) as distance_meter")])
                ->orderBy('distance_meter');
        }
    }

}